/**
* Copyright @ 2023 iAuto (Shanghai) Co., Ltd.
* All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are NOT permitted except as agreed by
* iAuto (Shanghai) Co., Ltd.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*/



const RES_SUCCESS = '0';
// 车系车型列表整合
export const carModelIntegrate = (data: any) => {
  const allCarModelMap: any = {};
  const carModelTree: any = [];

  for (const item of data) {
    const { sCode, seriesName, mCode, modelName } = item;
    const carModel = allCarModelMap[sCode];

    if (carModel) {
      carModel.children.push({
        value: mCode,
        label: modelName,
      });

      carModel.models[mCode] = {
        mCode,
        modelName,
      };
    } else {
      const newCarModel = {
        sCode,
        seriesName,
        value: sCode,
        label: seriesName,
        models: {
          [mCode]: {
            mCode,
            modelName,
          },
        },
        children: [
          {
            value: mCode,
            label: modelName,
          },
        ],
      };

      allCarModelMap[sCode] = newCarModel;
      carModelTree.push(newCarModel);
    }
  }

  return { carModelTree, allCarModelMap };
};

export const resCarMapList = (
  payload: any,
  allCarModelList: any[],
  allCarModelMap: any,
  publishedCarRes: any,
  unpublishCountName = 'unpublishCount',
  isPublishName = 'isPublish'
) => {
  const unpublishedCar = {};
  if (
    !publishedCarRes.error ||
    (publishedCarRes.error && publishedCarRes.error.code === RES_SUCCESS)
  ) {
    if (Array.isArray(publishedCarRes?.data?.detail) || Array.isArray(publishedCarRes?.data)) {
      const forListdat: any[] = publishedCarRes?.data?.detail || publishedCarRes?.data
      forListdat.forEach((detail: any) => {
        // const { sCode, unpublishCount } = detail;
        const sCode: any = detail.sCode
        const unpublishCount: any = detail[unpublishCountName]
           // eslint-disable-next-line
        if (unpublishedCar[sCode]) {
        } else {
          unpublishedCar[sCode] = {
            unpublishCount,
          };
          unpublishedCar[sCode].models = {};
          if (Array.isArray(detail.models)) {
            detail.models.forEach((model: any) => {
              // const { isPublish, socVersion } = model;
              const socVersion: any = model.socVersion
              const isPublish: any = model[isPublishName]
              unpublishedCar[sCode].models[model.mCode] = { isPublish, socVersion };
            });
          }
        }
      });
    }


    // 构造带红点的数据
    allCarModelList.forEach((series) => {
      const { value: sCode } = series;
      if (unpublishedCar[sCode]) {
        series.labelNum = unpublishedCar[sCode].unpublishCount || 0;
        allCarModelMap[sCode].unpublishCount = series.labelNum;
        if (Array.isArray(series.children)) {
          series.children.forEach((model: any) => {
            const { value: mCode } = model;
            if (
              unpublishedCar[sCode].models &&
              unpublishedCar[sCode].models[mCode]
            ) {
              model.showDot = !unpublishedCar[sCode].models[mCode].isPublish;
              model.socVersion = unpublishedCar[sCode].models[mCode].socVersion;
              allCarModelMap[sCode].models[mCode][isPublishName] = !model.showDot;
              allCarModelMap[sCode].models[mCode].socVersion = model.socVersion;
            } else {
              model.showDot = !!0;
            }
          });
        }
      }
    });
    publishedCarRes.data.allCarModelMap = allCarModelMap;
    publishedCarRes.data.allCarModelList = allCarModelList;
    publishedCarRes.data.unpublishTotal = publishedCarRes.data.unpublishTotal || 0;
  }
  return {
    ...publishedCarRes,
    data: {
      allCarModelMap, allCarModelList, unpublishTotal: publishedCarRes.data.unpublishTotal || 0
    }
  };
}
